package mark.tom.gear.base.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Build.VERSION
import com.hundsun.gmubase.crash.CrashUserInfo
import com.hundsun.gmubase.crash.LightCrashManager
import com.hundsun.gmubase.manager.GmuManager
import com.hundsun.gmubase.manager.HybridCore
import com.hundsun.gmubase.manager.ModuleVersion
import com.hundsun.gmubase.network.LightRequestHelper
import com.hundsun.gmubase.utils.BaseTool
import com.hundsun.gmubase.utils.LogUtils
import com.hundsun.gmubase.utils.SharedPreferencesManager
import mark.tom.gear.base.callback.CallBack

/**
 * @Description: Gmu工具类
 * @Author: ttmk110@163.com
 * @Package: mark.tom.gear.base.utils
 * @Date: 2021/1/15
 * @Copyright: 版权归 Mark 所有
 * <ModifyLog>
 * @ModifyContent:
 * @Author:
 * @Date:
 * </ModifyLog>
 */
object GmuUtil {

    /**
     * Description: 设置GMU日志等级为Debug
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/18 14:24
     */
    fun openDebug() {
        LogUtils.setLogLevel(LogUtils.DEBUG)
    }

    /**
     * Description: 初始化GMU混合开发内核
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/15 10:40 上午
     */
    fun initHybridCore(mContext: Context) {
        try {
            HybridCore.initHybridFramework(mContext)
        } catch (e: Exception) {
            e.message?.let { LogUtil.e(it) }
        }
    }

    /**
     * Description: 关闭系统低等级提示框
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/18 14:25
     */
    @SuppressLint("PrivateApi")
    fun closeAndroidPDialog() {
        if (VERSION.SDK_INT >= Build.VERSION_CODES.P && VERSION.SDK_INT != Build.VERSION_CODES.R) {
            var cls: Class<*>
            try {
                cls = Class.forName("android.content.pm.PackageParser\$Package")
                val declaredConstructor = cls.getDeclaredConstructor(String::class.java)
                declaredConstructor.setAccessible(true)
            } catch (e: Exception) {
                e.message?.let { LogUtil.e(it) }
            }
            try {
                cls = Class.forName("android.app.ActivityThread")
                val declaredMethod = cls.getDeclaredMethod("currentActivityThread")
                declaredMethod.isAccessible = true
                val activityThread = declaredMethod.invoke(null as Any?)
                val mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown")
                mHiddenApiWarningShown.isAccessible = true
                mHiddenApiWarningShown.setBoolean(activityThread, true)
            } catch (e: Exception) {
                e.message?.let { LogUtil.e(it) }
            }
        }
    }

    /**
     * Description: 初始化崩溃日志采集服务
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/18 14:26
     */
    fun initCrashService(context: Context) {
        // 设置默认框架版本号
        val info = CrashUserInfo()
        info.fwVersion = ModuleVersion.version
        // 设置自定义渠道号，默认读取AndroidManifest.xml中UBAS_CHANNEL字段值
        info.channel = BaseTool.getApplicationMetaData(context, "UBAS_CHANNEL", "")
        LightCrashManager.getInstance().setUserInfo(info)
        LightCrashManager.getInstance().initServiceWithAppkey(context, "", "")
    }

    /**
     * Description: 发送崩溃日志至服务端
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/19 11:03
     */
    fun sendStartInfo2Server(context: Context) {
        val license = LightRequestHelper.getInstance().getLightLicenseContent(context)
        var ubasKeyAndroid = if (license != null) {
            license.optString("ubasKeyAndroid", "")
        } else {
            ""
        }
        if ("" == ubasKeyAndroid) {
            ubasKeyAndroid = SharedPreferencesManager.getStringPreferenceSaveValue("light_ubasKey")
        }
        val ubasServer = SharedPreferencesManager.getStringPreferenceSaveValue("ubasserver")
        LightCrashManager.getInstance().resetServiceWithAppkey(context, ubasKeyAndroid, ubasServer)
    }

    /**
     * Description: 初始化GMU Manager
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/1/19 11:01
     */
    fun initManager(mCallBack: CallBack<Any, Unit>?) {
        GmuManager.getInstance().init { result: Boolean ->
            if (result) mCallBack?.onSuccess("") else mCallBack?.onFail("")
        }
    }

    /**
     * Description: 初始化GMU Service
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/4/2 16:33
     */
    fun initService(mActivity: Activity) {
        GmuManager.initService(mActivity)
    }

    /**
     * Description: 更新离线包数据
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/4/2 16:33
     */
    fun updateOfflinePackage(mContext: Context) {
        if (null == GmuManager.getInstance().offlineUpdateManager) {
            return
        }
        GmuManager.getInstance().offlineUpdateManager.fullPackUpdate(mContext)
    }
}